package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.*;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SchoolConsumeMapper;
import com.ruoyi.system.mapper.SchoolPayrecorddMapper;
import com.ruoyi.system.mapper.SchoolStudentMapper;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SchoolPayrecordMapper;
import com.ruoyi.system.service.ISchoolPayrecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 缴费记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-19
 */
@Service
@Transactional
public class SchoolPayrecordServiceImpl implements ISchoolPayrecordService 
{
    @Autowired
    private SchoolPayrecordMapper schoolPayrecordMapper;

    @Autowired
    private SchoolPayrecorddMapper schoolPayrecorddMapper;

    @Autowired
    private SchoolConsumeMapper schoolConsumeMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private SchoolStudentMapper schoolStudentMapper;

    /**
     * 查询缴费记录
     * 
     * @param id 缴费记录ID
     * @return 缴费记录
     */
    @Override
    public SchoolPayrecord selectSchoolPayrecordById(Long id)
    {
        return schoolPayrecordMapper.selectSchoolPayrecordById(id);
    }

    /**
     * 查询缴费记录列表
     * 
     * @param schoolPayrecord 缴费记录
     * @return 缴费记录
     */
    @Override
    public List<SchoolPayrecord> selectSchoolPayrecordList(SchoolPayrecord schoolPayrecord)
    {
        return schoolPayrecordMapper.selectSchoolPayrecordList(schoolPayrecord);
    }

    @Override
    public List<SchoolPayrecordTj> selectSchoolPayrecordListTj(SchoolPayrecord schoolPayrecord)
    {
        return schoolPayrecordMapper.selectSchoolPayrecordListTj(schoolPayrecord);
    }

    @Override
    public List<SchoolPayrecordTj> selectSchoolPayrecordListPayTj(SchoolPayrecord schoolPayrecord)
    {
        return schoolPayrecordMapper.selectSchoolPayrecordListPayTj(schoolPayrecord);
    }

    @Override
    public List<SchoolPayrecordTj> selectSchoolPayrecordListPayTjPm(SchoolPayrecord schoolPayrecord)
    {
        List<SchoolPayrecordTj> list = schoolPayrecordMapper.selectSchoolPayrecordListPayTjPm(schoolPayrecord);
        for (SchoolPayrecordTj obj:list) {
            SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(obj.getSchoolId()));
            obj.setSchoolName(sysDept.getDeptName());
            SchoolPayrecord paramPay = new SchoolPayrecord();
            paramPay.setSchoolId(Long.valueOf(obj.getSchoolId()));
            List<SchoolPayrecord> payList = schoolPayrecordMapper.selectSchoolPayrecordList(paramPay);
            BigDecimal pay = BigDecimal.valueOf(0);
            for (SchoolPayrecord payObj:payList) {
                pay = pay.add(payObj.getAmount());
            }
            obj.setPay(pay);
            SchoolConsume paramConsume = new SchoolConsume();
            paramConsume.setSchoolId(Long.valueOf(obj.getSchoolId()));
            List<SchoolConsume> consumeList = schoolConsumeMapper.selectSchoolConsumeList(paramConsume);
            BigDecimal consume = BigDecimal.valueOf(0);
            for (SchoolConsume consumeObj:consumeList) {
                consume = consume.add(consumeObj.getConsumeAmount());
            }
            obj.setConsume(consume);
        }
        return list;
    }

    /**
     * 新增缴费记录
     * 
     * @param schoolPayrecord 缴费记录
     * @return 结果
     */
    @Override
    public int insertSchoolPayrecord(SchoolPayrecord schoolPayrecord)
    {
        Date curD = DateUtils.getNowDate();
        if (schoolPayrecord.getStudentId()==0) {
            this.insertSchoolPayrecordUser(schoolPayrecord);
            SysUser user = new SysUser();
            user.setPhonenumber(schoolPayrecord.getPhonenumber());
            user.setDeptId(schoolPayrecord.getSchoolId());
            SysUser info = userMapper.checkUPhoneUnique(user);
            if (null!=schoolPayrecord.getClassesId()&&schoolPayrecord.getClassesId()>0) {
                Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
                SchoolStudent schoolStudent = new SchoolStudent();
                schoolStudent.setSchoolId(schoolPayrecord.getSchoolId());
                schoolStudent.setClassesId(schoolPayrecord.getClassesId());
                schoolStudent.setStudentId(info.getUserId());
                schoolStudent.setTeacherId(userId);
                schoolStudent.setCreateTime(curD);
                schoolStudentMapper.insertSchoolStudent(schoolStudent);
            }
            schoolPayrecord.setStudentId(info.getUserId());
        }
        SchoolPayrecord params = new SchoolPayrecord();
        params.setSchoolId(schoolPayrecord.getSchoolId());
        params.setStudentId(schoolPayrecord.getStudentId());
        List<SchoolPayrecord> payList = schoolPayrecordMapper.selectSchoolPayrecordList(params);
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        SchoolPayrecordd schoolPayrecordd = new SchoolPayrecordd();
        schoolPayrecordd.setSchoolId(schoolPayrecord.getSchoolId());
        schoolPayrecordd.setStudentId(schoolPayrecord.getStudentId());
        schoolPayrecordd.setAmount(schoolPayrecord.getAmount());
        schoolPayrecordd.setRemark("追缴");
        schoolPayrecordd.setTeacherId(userId);
        schoolPayrecordd.setCreateTime(curD);
        schoolPayrecorddMapper.insertSchoolPayrecordd(schoolPayrecordd);
        if (null!=payList&&payList.size()>0) {
            SchoolPayrecord schoolPayrecordU = payList.get(0);
            BigDecimal newAmount = schoolPayrecordU.getAmount().add(schoolPayrecord.getAmount());
            BigDecimal remainder = schoolPayrecordU.getRemainder().add(schoolPayrecord.getAmount());
            schoolPayrecordU.setAmount(newAmount);
            schoolPayrecordU.setNum(schoolPayrecordU.getNum()+schoolPayrecord.getNum());
            schoolPayrecordU.setRemainder(remainder);
            schoolPayrecordU.setRemaindnum(schoolPayrecordU.getRemaindnum()+schoolPayrecord.getNum());
            schoolPayrecordU.setTeacherId(userId);
            schoolPayrecordU.setCreateTime(curD);
            return schoolPayrecordMapper.updateSchoolPayrecord(schoolPayrecordU);
        }else {
            SysUser sysUser = userMapper.selectUserById(schoolPayrecord.getStudentId());
            if (null!=sysUser) {
                schoolPayrecord.setXsname(sysUser.getUserName());
                schoolPayrecord.setXsphone(sysUser.getPhonenumber());
                schoolPayrecord.setRemainder(schoolPayrecord.getAmount());
                schoolPayrecord.setRemaindnum(schoolPayrecord.getNum());
                schoolPayrecord.setTeacherId(userId);
                schoolPayrecord.setCreateTime(curD);
                return schoolPayrecordMapper.insertSchoolPayrecord(schoolPayrecord);
            }else {
                return 0;
            }
        }
    }

    public void insertSchoolPayrecordUser(SchoolPayrecord schoolPayrecord)
    {
        if (null!=schoolPayrecord.getStudentName()&&!"".equals(schoolPayrecord.getStudentName())) {
            SysUser user = new SysUser();
            user.setDeptId(schoolPayrecord.getSchoolId());
            user.setUserName(schoolPayrecord.getStudentName());
            user.setSex(schoolPayrecord.getSex());
            user.setPhonenumber(schoolPayrecord.getPhonenumber());
            userMapper.insertUser(user);
            SysRole role = roleMapper.checkRoleKeyUnique("common");
            SysUserRole ur = new SysUserRole();
            ur.setUserId(user.getUserId());
            ur.setRoleId(role.getRoleId());
            List<SysUserRole> listR = new ArrayList<SysUserRole>();
            listR.add(ur);
            userRoleMapper.batchUserRole(listR);
            SysPost post = postMapper.getPostCodeUnique("xuesheng");
            SysUserPost up = new SysUserPost();
            up.setUserId(user.getUserId());
            up.setPostId(post.getPostId());
            List<SysUserPost> listP = new ArrayList<SysUserPost>();
            listP.add(up);
            userPostMapper.batchUserPost(listP);
        }
    }

    /**
     * 修改缴费记录
     * 
     * @param schoolPayrecord 缴费记录
     * @return 结果
     */
    @Override
    public int updateSchoolPayrecord(SchoolPayrecord schoolPayrecord)
    {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        SysUser sysUser = userMapper.selectUserById(schoolPayrecord.getStudentId());
        if (null==sysUser) {
            throw new CustomException("学生已不存在");
        }
        SchoolConsume paramConsume = new SchoolConsume();
        paramConsume.setSchoolId(sysUser.getDeptId());
        paramConsume.setStudentId(sysUser.getUserId());
        List<SchoolConsume> consumeList = schoolConsumeMapper.selectSchoolConsumeList(paramConsume);
        BigDecimal consume = BigDecimal.valueOf(0);
        for (SchoolConsume consumeObj:consumeList) {
            consume = consume.add(consumeObj.getConsumeAmount());
        }
        Date curD = DateUtils.getNowDate();
        SchoolPayrecord schoolPayrecordY = schoolPayrecordMapper.selectSchoolPayrecordById(schoolPayrecord.getId());
        if (schoolPayrecordY.getAmount().compareTo(schoolPayrecord.getAmount())!=0||
            schoolPayrecordY.getRealAmount().compareTo(schoolPayrecord.getRealAmount())!=0) {
            SchoolPayrecordd schoolPayrecordd = new SchoolPayrecordd();
            schoolPayrecordd.setSchoolId(schoolPayrecord.getSchoolId());
            schoolPayrecordd.setStudentId(schoolPayrecord.getStudentId());
            if (schoolPayrecordY.getAmount().compareTo(schoolPayrecord.getAmount())!=0) {
                schoolPayrecordd.setOldamount(schoolPayrecordY.getAmount());
                schoolPayrecordd.setAmount(schoolPayrecord.getAmount());
            }else {
                schoolPayrecordd.setOldamount(null);
                schoolPayrecordd.setAmount(null);
            }
            if (schoolPayrecordY.getRealAmount().compareTo(schoolPayrecord.getRealAmount())!=0) {
                schoolPayrecordd.setRealOldamount(schoolPayrecordY.getRealAmount());
                schoolPayrecordd.setRealAmount(schoolPayrecord.getRealAmount());
            }else {
                schoolPayrecordd.setRealOldamount(null);
                schoolPayrecordd.setRealAmount(null);
            }
            schoolPayrecordd.setRemark("修改");
            schoolPayrecordd.setTeacherId(userId);
            schoolPayrecordd.setCreateTime(curD);
            schoolPayrecorddMapper.insertSchoolPayrecordd(schoolPayrecordd);
        }
        BigDecimal remainder = schoolPayrecord.getAmount().subtract(consume);
        schoolPayrecord.setXsname(sysUser.getUserName());
        schoolPayrecord.setXsphone(sysUser.getPhonenumber());
        schoolPayrecord.setRemainder(remainder);
        schoolPayrecord.setRemaindnum(schoolPayrecord.getNum()-consumeList.size());
        schoolPayrecord.setTeacherId(userId);
        schoolPayrecord.setCreateTime(curD);
        return schoolPayrecordMapper.updateSchoolPayrecord(schoolPayrecord);
    }

    /**
     * 批量删除缴费记录
     * 
     * @param ids 需要删除的缴费记录ID
     * @return 结果
     */
    @Override
    public int deleteSchoolPayrecordByIds(Long[] ids)
    {
        return schoolPayrecordMapper.deleteSchoolPayrecordByIds(ids);
    }

    /**
     * 删除缴费记录信息
     * 
     * @param id 缴费记录ID
     * @return 结果
     */
    @Override
    public int deleteSchoolPayrecordById(Long id)
    {
        return schoolPayrecordMapper.deleteSchoolPayrecordById(id);
    }
}
